package com.ls.mysql.dao;

import com.ls.mysql.entity.YzNotice;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Maple.Chen
 * @Description 实现文件上传
 * @Date 11:00 2020/5/21
 **/
@Mapper
@Component
public interface NoticeMapper {
    // 查看文件
    @Select("select * from yz_notice order by priority desc")
    @Results(id = "order1", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "priority", property = "priority", jdbcType = JdbcType.INTEGER),
            @Result(column = "path", property = "path", jdbcType = JdbcType.VARCHAR),
            @Result(column = "date", property = "postTime", jdbcType = JdbcType.DATE),
            @Result(column = "uid", property = "yzUsers", one = @One(select = "com.ls.mysql.dao.UserDao.queryUserById"))
    })
    public List<YzNotice> queryAllNotice();

    // 添加公告
    @SelectKey(keyProperty = "notice.id", resultType = String.class, before = true,
            statement = "select replace(uuid(), '-', '')")
    @Options(keyProperty = "notice.id", useGeneratedKeys = true)
    @Insert({"<script>",
            "   insert into yz_notice(id,title,priority,path,uid,date)",
            "   values(#{notice.id},#{notice.title},#{notice.priority},#{notice.path},#{notice.yzUsers.id},#{notice.postTime})",
            "</script>"})
    public int saveNotice(@Param("notice") YzNotice yzNotice);

    // 删除公告
    @Delete("delete from yz_notice n where n.id=#{id}")
    public int delNotice(String id);

    // 查看某条公告
    @Select("select * from yz_notice n where n.id=#{id}")
    public YzNotice queryNoticeById(String id);

}
